package com.distribution.system.controller.system;

import com.distribution.common.annotation.Log;
import com.distribution.common.core.controller.BaseController;
import com.distribution.common.core.domain.AjaxResult;
import com.distribution.common.core.page.TableDataInfo;
import com.distribution.common.enums.BusinessType;
import com.distribution.system.domain.SysNotice;
import com.distribution.system.domain.entity.NoticeUserManage;
import com.distribution.system.service.ISysNoticeService;
import com.distribution.system.service.NoticeUserManageService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 公告 信息操作处理
 *
 * @author ruoyi
 */
@RestController
@RequestMapping("/system/notice")
public class SysNoticeController extends BaseController
{
    @Resource
    private ISysNoticeService noticeService;
    @Resource
    private NoticeUserManageService noticeUserManageService;

    /**
     * 新增通知公告
     */
    @PreAuthorize("@ss.hasRole('operationSupervisor')")
    @Log(title = "通知公告", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Validated @RequestBody SysNotice notice)
    {
        notice.setCreateBy(getUsername());
        return toAjax(noticeService.insertNotice(notice));
    }

    /**
     * 删除通知公告
     */
    @PreAuthorize("@ss.hasRole('operationSupervisor')")
    @Log(title = "通知公告", businessType = BusinessType.DELETE)
    @DeleteMapping("/{noticeId}")
    public AjaxResult remove(@PathVariable Long noticeId)
    {
        return toAjax(noticeService.deleteNoticeById(noticeId));
    }

    /**
     * 已读
     */
    @PostMapping("/markRead")
    public AjaxResult markRead(@RequestBody SysNotice notice)
    {
        noticeUserManageService.markReadByNoticeId(notice.getNoticeId());
        return success();
    }

    /**
     * 修改通知公告
     */
    @PreAuthorize("@ss.hasRole('operationSupervisor')")
    @Log(title = "通知公告", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@Validated @RequestBody SysNotice notice)
    {
        notice.setUpdateBy(getUsername());
        return toAjax(noticeService.updateNotice(notice));
    }

    /**
     * 获取最近一条通知
     */
    @GetMapping("/getLatestNotice")
    public AjaxResult getLatestNotice()
    {
        SysNotice notice = noticeService.selectLatestNotice();
        return success(notice);
    }

    /**
     * 获取最近一条公告
     */
    @GetMapping("/getLatestGongGao")
    public AjaxResult getLatestGongGao()
    {
        List<SysNotice> noticeList = noticeService.lambdaQuery()
                .eq(SysNotice::getNoticeType, "2")
                .eq(SysNotice::getStatus, "0")
                .orderByDesc(SysNotice::getUpdateTime).list();
        if (noticeList.isEmpty() || noticeList.get(0) == null) return success(null);
        return success(noticeList.get(0));
    }

    /**
     * 获取通知公告列表
     */
    @PreAuthorize("@ss.hasRole('operationSupervisor')")
    @GetMapping("/list")
    public TableDataInfo list(SysNotice notice)
    {
        startPage();
        List<SysNotice> list = noticeService.selectNoticeList(notice);
        return getDataTable(list);
    }

    /**
     * 根据通知公告编号获取详细信息
     */
    @PreAuthorize("@ss.hasRole('operationSupervisor')")
    @GetMapping(value = "/{noticeId}")
    public AjaxResult getInfo(@PathVariable Long noticeId)
    {
        return success(noticeService.selectNoticeById(noticeId));
    }

    /**
     * 获取通知公告
     */
    @GetMapping("/noticeList")
    public AjaxResult getNoticeList()
    {
        List<NoticeUserManage> noticeUserManageList = noticeUserManageService.lambdaQuery()
                .eq(NoticeUserManage::getUserId, getUserId())
                .eq(NoticeUserManage::getStatus, "0")
                .list();
        List<Long> noticeIdList = noticeUserManageList.stream().map(NoticeUserManage::getNoticeId).collect(Collectors.toList());
        if (noticeIdList.isEmpty()) return success();
        List<SysNotice> noticeList = noticeService.lambdaQuery()
                .in(SysNotice::getNoticeId, noticeIdList)
                .eq(SysNotice::getStatus, "0")
                .orderByDesc(SysNotice::getUpdateTime)
                .list();
        return success(noticeList);
    }

//    @PostMapping("/")
}
